package voliate;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Visible {
    volatile static boolean run = true;

    //锁对象，也可用synchronized锁住lock使线程t停下来
    //synchronized可以保证原子性和可见性，但是synchronized是重量级锁,耗费性能
    final static Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            while(true){
                if(!run){
                    break;
                }
            }
        });
        t.start();
        Thread.sleep(1000);
        log.debug("停止");
        System.out.println();
        run = false;//不用voliate，线程t不会如预想的停止下来
    }

}
